package org.aristotle.auth.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.aristotle.auth.client.UserClient;
import org.aristotle.auth.service.UserService;
import org.aristotle.common.response.result.Result;
import org.aristotle.common.response.result.ResultCode;
import org.aristotle.user.model.dto.UserDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserClient userClient;

    @Override
    public UserDTO getByUsername(String username) {
        Result<?> result = userClient.getUserByUsername(username);

        if (ResultCode.QUERY_SUCCESS.getCode() == result.getCode().intValue()) {
            return JSONObject.parseObject(JSON.toJSONString(result.getData()), UserDTO.class);
        } else {
            return null;
        }
    }
}
